"""
Copyright (c) 2022 Huawei Technologies Co.,Ltd.

openGauss is licensed under Mulan PSL v2.
You can use this software according to the terms and conditions of the Mulan PSL v2.
You may obtain a copy of Mulan PSL v2 at:

          http://license.coscl.org.cn/MulanPSL2

THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
See the Mulan PSL v2 for more details.
"""
"""
Case Type   : 故障&可靠性测试
Case Name   : 恢复同一表过程中删除数据
Create At   : 2020/11/24
@zou_jialiang0505328126
Description :
    1.在数据库postgres中创建表
    2.使用gs_dump导出该表
    3. 使用gs_restore恢复该表
    4.删除数据
    5.检查数据一致性
    6.执行TPCC
    7.检查数据一致性
Expect      :
    1.创建表成功
    2.导出成功
    3.恢复成功
    4.删除数据成功
    5.数据一致，该表大于500000的数据小于等于20
    6.执行tpcc成功
    7.数据一致
History     :
    modified: 2020/12/14  modified by @wan005,修改用例描述，修改tpcc用户
    modified: 2021/08/16  modified by @wan005,删除-c参数
"""

import os
import unittest
import sys
import time
from yat.test import macro
from yat.test import Node

sys.path.append(sys.path[0] + "/../")
from testcase.utils.CommonSH import *
from testcase.utils.Common import Common
from testcase.utils.Constant import Constant
from testcase.utils.Logger import Logger
from testcase.utils.ComThread import ComThread


class Basebackup(unittest.TestCase):
    commonshpri = CommonSH('PrimaryDbUser')
    commonshsta = CommonSH('Standby1DbUser')
    commonshsta2 = CommonSH('Standby2DbUser')
    nodes_tuple = ('PrimaryDbUser', 'Standby1DbUser', 'Standby2DbUser')

    @RestartDbCluster(*nodes_tuple)
    def setUp(self):
        self.log = Logger()
        self.log.info('------------Opengauss_Reliability_Tools_Restore_Case023.py 开始执行------------')
        self.dbPrimaryDbUser = Node(node='PrimaryDbUser')
        self.dbStandby1DbUser = Node(node='Standby1DbUser')
        self.dbStandby2DbUser = Node(node='Standby2DbUser')
        self.dbPrimaryRootNode = Node(node='PrimaryRoot')
        self.Constant = Constant()
        self.common = Common('PrimaryRoot')
        self.restore_file = os.path.join(macro.DB_BACKUP_PATH, 'dump_backup', 'tools_restore_case001.tar')
        self.sql_path = os.path.join(macro.DB_BACKUP_PATH, 'testscript')
        self.tblname = 'restore_case023'
        self.values = '2000000'
        self.idxname = 'restore_case023_idx'

    def test_basebackup(self):
        self.log.info('-------------------创建备份路径-----------------------------------')
        shell_cmd = f'rm -rf {os.path.dirname(self.restore_file)}; mkdir {os.path.dirname(self.restore_file)}'
        self.log.info(shell_cmd)
        result = self.dbPrimaryDbUser.sh(shell_cmd).result()
        self.log.info(result)

        self.log.info('----------------------------------------创建表插入数据------------------------------')
        text = '----step1：创建表插入数据 expect:成功----'
        result = self.commonshpri.executDbSql(f'create table {self.tblname}(i int)\
                                                        partition by range (i)\
                                                        (\
                                                             partition p1 values less than (100000),\
                                                             partition p2 values less than (200000),\
                                                             partition p3 values less than (2000001)\
                                                        );insert into {self.tblname} (select * from generate_series(1,{self.values}))')
        self.log.info(result)
        self.assertIn('CREATE', result, '执行失败:' + text)

        self.log.info('--------------------导出数据----------------------------')
        text = '----step2：导出数据 expect:成功----'
        result = self.commonshpri.dumpFile(filename=self.restore_file, cmd=f'-F t -t {self.tblname}', get_detail=False)
        self.log.info(result)
        self.assertTrue(result, '执行失败:' + text)

        self.log.info('--------------------获取还原前数据库已有表------------------------')
        self.table_list = self.commonshpri.executDbSql(r'\d')
        self.log.info(self.table_list)

        self.log.info('------------------起线程恢复数据-----------------------------------')
        restore_thread = ComThread(self.commonshpri.restoreFile,
                                   args=(self.restore_file, '', ))
        restore_thread.setDaemon(True)
        restore_thread.start()

        self.log.info('-------------------删除数据-----------------------------------')
        text = '----step4：删除数据 expect:成功----'
        result = self.commonshpri.executDbSql(
            f'delete {self.tblname} where i<=200000;')
        self.log.info(result)
        self.assertIn('DELETE', result, '执行失败:' + text)

        self.log.info('-------------------------检查恢复数据结果------------------------')
        text = '----step3：起线程恢复数据 expect:成功----'
        restore_thread.join(60 * 10)
        result = restore_thread.get_result()
        self.assertIn(self.Constant.RESTORE_SUCCESS_MSG, result,
                      '执行失败:' + text)

        self.log.info('----------------------------对比恢复前后数据是否一致-------------------------')
        text = '----step4：对比恢复前后数据是否一致 expect:成功----'
        time.sleep(5)
        self.table_list_after = self.commonshpri.executDbSql(r'\d')
        self.log.info(self.table_list_after)
        self.assertIn(self.table_list, self.table_list_after, '执行失败:' + text)

        self.log.info('-------------------检查主备是否同步-------------------')
        text = '----step5：检查主备是否同步 expect:成功----'
        result = self.commonshsta.check_data_consistency()
        self.assertTrue(result, '执行失败:' + text)
        result = self.commonshsta2.check_data_consistency()
        self.assertTrue(result, '执行失败:' + text)

        self.log.info('--------------------检查数据一致性---------------------')
        text = '----step6：检查数据一致性 expect:成功----'
        nodes_tuple = (self.dbPrimaryDbUser, self.dbStandby1DbUser, self.dbStandby2DbUser)
        flag = self.common.check_data_sample_by_all(r'\d', *nodes_tuple)
        self.assertTrue(flag, '执行失败:' + text)

        table_dict = self.common.format_sql_result(self.commonshpri.executDbSql(r'\d'))
        table_name = table_dict.get('Name')
        for name in table_name:
            select_sql = f'select count(*) from {name};'
            flag = self.common.check_data_sample_by_all(select_sql, *nodes_tuple)
            self.assertTrue(flag, '执行失败:' + text)

        self.log.info('------------------启动TPCC----------------------------')
        text = '----step7：启动TPCC expect:成功----'
        result = self.common.startTPCC(self.dbPrimaryRootNode, macro.TPCC_PATH)
        self.log.info(result)
        self.assertIn(self.Constant.TPCC_SUCCESS_MSG, result, '执行失败:' + text)

        self.log.info('-------------------检查主备是否同步-------------------')
        text = '----step8：检查主备是否同步 expect:成功----'
        result = self.commonshsta.check_data_consistency()
        self.assertTrue(result, '执行失败:' + text)
        result = self.commonshsta2.check_data_consistency()
        self.assertTrue(result, '执行失败:' + text)

        self.log.info('--------------------检查数据一致性---------------------')
        text = '----step9：检查数据一致性 expect:成功----'
        nodes_tuple = (self.dbPrimaryDbUser, self.dbStandby1DbUser, self.dbStandby2DbUser)
        flag = self.common.check_data_sample_by_all(r'\d', *nodes_tuple)
        self.assertTrue(flag, '执行失败:' + text)

        table_dict = self.common.format_sql_result(self.commonshpri.executDbSql(r'\d'))
        table_name = table_dict.get('Name')
        for name in table_name:
            select_sql = f'select count(*) from {name};'
            flag = self.common.check_data_sample_by_all(select_sql, *nodes_tuple)
            self.assertTrue(flag, '执行失败:' + text)

    def tearDown(self):
        self.log.info('-------------------------环境清理------------------------')
        self.log.info('--------------------------删除备份文件------------------')
        shell_cmd = f'rm -rf {os.path.dirname(self.restore_file)}'
        self.log.info(shell_cmd)
        result = self.dbPrimaryDbUser.sh(shell_cmd).result()
        self.log.info(result)

        self.log.info('----------------------删除执行脚本路径----------------------------')
        shell_cmd = f'rm -rf {self.sql_path}'
        self.log.info(shell_cmd)
        result = self.dbPrimaryDbUser.sh(shell_cmd).result()
        self.log.info(result)

        self.log.info('--------------------------删除表------------------------------')
        result = self.commonshpri.executDbSql(
            f'drop index if exists {self.idxname};drop table if exists {self.tblname};')
        self.log.info(result)
        self.log.info('----------Opengauss_Reliability_Tools_Restore_Case023.py 执行结束-----------------------')
